package com.avic.rong.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.adapter.show.AnLiHAdapter;
import com.avic.rong.bean.show_bean.AnLiBean;
import com.avic.rong.bean.show_bean.EsInfoBean;
import com.avic.rong.fragment.showapi.AnLiApi;
import com.avic.rong.fragment.showapi.EsInfoApi;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 案例分享
 */
public class AnLiShareActivity extends BaseActivity {

    @BindView(R.id.an_li_rec)
    RecyclerView an_li_rec;
    @BindView(R.id.anli_Title_back)
    ImageView anliTitleBack;


    private ArrayList<AnLiBean.DataBean> mList = new ArrayList<>();
    private AnLiHAdapter anLiHAdapter;
    private ArrayList<AnLiBean.DataBean> channelList = new ArrayList<>();


    @Override
    protected int getContentViewId() {
        return R.layout.activity_an_li;
    }

    @Override
    protected void initView() {

        ButterKnife.bind(this);
        anliTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        anLiHAdapter = new AnLiHAdapter(AnLiShareActivity.this);


        AnLiApi anLiApi = new AnLiApi(this, httpOnNextListener);
        anLiApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(anLiApi);
    }

    @Override
    protected void addListener() {

    }

    HttpOnNextListener<BaseResultEntity> httpOnNextListener = new HttpOnNextListener<BaseResultEntity>() {

        @Override
        public void onNext(BaseResultEntity baseResultEntity) {

            List<AnLiBean> data = (List<AnLiBean>) baseResultEntity.getData();
            if (data != null) {
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i) != null) {
                        if (data.get(i).getType() == 5) {
                            List<AnLiBean.DataBean> dataX = data.get(i).getDataX();
                            channelList.addAll(dataX);
                            DividerItemDecoration divider = new DividerItemDecoration(AnLiShareActivity.this, DividerItemDecoration.VERTICAL);
                            divider.setDrawable(ContextCompat.getDrawable(AnLiShareActivity.this, R.drawable.divider_05_pix));
                            an_li_rec.addItemDecoration(divider);
                            an_li_rec.setLayoutManager(new LinearLayoutManager(AnLiShareActivity.this));
                            an_li_rec.setAdapter(anLiHAdapter);
                        }
                    }
                }
            }
            String substring = "";
            String contIds = "";
            for (int i = 0; i < channelList.size(); i++) {
                if (channelList.get(i) != null) {
                    long contId = channelList.get(i).getDataId();
                    contIds += contId + ",";
                }
            }
            if (contIds.length() > 0) {
                substring = contIds.substring(0, contIds.length() - 1);
            }
            EsInfoApi esInfoApi = new EsInfoApi(AnLiShareActivity.this, EnsOnNextListener, substring);
            esInfoApi.setCache(false);
            HttpManager.getInstance().doHttpDeal(esInfoApi);
        }
    };

    //搜索补全接口
    HttpOnNextListener<BaseResultEntity> EnsOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            String code = baseResultEntity.getCode();
            if (code.equals("200")) {
                ArrayList<EsInfoBean> esInfoBean = (ArrayList<EsInfoBean>) baseResultEntity.getData();
                anLiHAdapter.setBean(channelList);
                anLiHAdapter.setEsInfoBeans(esInfoBean);
                anLiHAdapter.notifyDataSetChanged();
            } else {
                Toast.makeText(AnLiShareActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    };

}
